package common;

import io.github.bonigarcia.wdm.WebDriverManager;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.edge.EdgeOptions;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.Duration;

public class utilsEdge {
    public static WebDriver driver;

    public utilsEdge(String string){
        driver=createDriver();
        driver.get(string);
    }

    public WebDriver createDriver(){
        if (driver==null){
            WebDriverManager.edgedriver().setup();
            EdgeOptions options=new EdgeOptions();
            options.addArguments("--remote-allow-origins=*");
            driver=new EdgeDriver(options);

            driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3));
        }
        return driver;
    }
    //屏幕截图
    public void getScreenShot(String str) throws IOException {
        SimpleDateFormat sim1=new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sim2=new SimpleDateFormat("HHmmssSS");
        String dirTime=sim1.format(System.currentTimeMillis());
        String fileTime=sim2.format(System.currentTimeMillis());
        String filename ="./src/test/image/"+ dirTime +"/" + str + "-" + fileTime+".png";
        File srcFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(srcFile,new File(filename));
    }
}
